<?php

use Carbon\Carbon;
use Carbon\CarbonInterval;
use Nyg\Holiday;
use PHPHtmlParser\Dom;

if (!function_exists('isHoliday')) {
    function isHoliday($time)
    {
        // 当前时间
        $currTime = $time;
        //工作日时间段
        $checkDayStr = date('Y-m-d ', $currTime);
        $straTime = strtotime($checkDayStr . "08:30");
        $endime = strtotime($checkDayStr . "17:30");

        $holiday = new holiday();

        $result = $holiday->isHoliday($currTime);

        if ($result) {
            $isHolidy = 1;  //休息了
        } else {
            if ($currTime >= $straTime && $currTime <= $endime) {
                $isHolidy = 0;  //没休息
            } else {
                $isHolidy = 1;  //休息了
            }
        }
        return $isHolidy;
    }
}

if (!function_exists('http_url_to_https')) {
    function http_url_to_https($url)
    {
        if (Str::startsWith($url, 'http') && !Str::startsWith($url, 'https')) {
            return 'https' . Str::after($url, 'http');
        }
        return $url;
    }
}

if (!function_exists('unique_random_number')) {
    function unique_random_number($min, $max, $num)
    {
        $count = 0;
        $return = array();
        while ($count < $num) {
            $return[] = mt_rand($min, $max);
            $return = array_flip(array_flip($return));
            $count = count($return);
        }
        shuffle($return);
        return $return;
    }
}

if (!function_exists('per')) {
    function per($per = null)
    {
        if (request('per')) {
            return min((int)request('per'), 100);
        }
        if ($per) {
            return min((int)$per, 100);
        }
        return 10;
    }
}

if (!function_exists('get_deep_array_value')) {
    function get_deep_array_value(array $path, array $deepArray)
    {
        $reduce = function (array $xs, $x) {
            return (
            array_key_exists($x, $xs)
            ) ? $xs[$x] : null;
        };
        return array_reduce($path, $reduce, $deepArray);
    }
}

if (!function_exists('array_keys_exists')) {
    function array_keys_exists(array $keys, array $arr)
    {
        return !array_diff_key(array_flip($keys), $arr);
    }
}

if (!function_exists('format_number_with_prefix_zero')) {
    function format_number_with_prefix_zero($number)
    {
        if ($number < 10) {
            return '000' . $number;
        } elseif ($number < 100) {
            return '00' . $number;
        } elseif ($number < 100) {
            return '0' . $number;
        } else {
            return $number;
        }
    }
}

if (!function_exists('get_distance_by_two_points')) {
    function get_distance_by_two_points($lng1, $lat1, $lng2, $lat2)
    {
        $radLat1 = deg2rad($lat1);
        $radLat2 = deg2rad($lat2);
        $radLng1 = deg2rad($lng1);
        $radLng2 = deg2rad($lng2);
        $a = $radLat1 - $radLat2;
        $b = $radLng1 - $radLng2;
        return 2 * asin(sqrt(pow(sin($a / 2), 2) + cos($radLat1) * cos($radLat2) * pow(sin($b / 2), 2)))
            * 6378.137 * 1000;
    }
}

/**
 * 二维数组根据某个字段排序
 * @param array $array 要排序的数组
 * @param string $keys 要排序的键字段
 * @param string $sort 排序类型 SORT_ASC SORT_DESC
 * @return array 排序后的数组
 */
if (!function_exists('arraySort')) {
    function arraySort($array, $keys, $sort = SORT_ASC)
    {
        $keysValue = [];
        foreach ($array as $k => $v) {
            $keysValue[$k] = $v[$keys];
        }
        array_multisort($keysValue, $sort, $array);
        return $array;
    }
}

if (!function_exists('format_han_number')) {
    function format_han_number($value)
    {
        if ($value > 10000) {
            return intval(ceil($value / 10000)) . "w+";
        }
        return $value;
    }
}

if (!function_exists('convert_img_tag')) {
    function convert_img_tag($html)
    {
        try {
            $dom = new Dom();
            $dom->loadStr($html);
            $imgDoms = $dom->find('img');
            foreach ($imgDoms as $imgDom) {
                $imgDom->removeAttribute('width');
                $imgDom->removeAttribute('height');
                $imgDom->removeAttribute('stype');
                $imgDom->setAttribute('style', "width:100%;max-width:100%");
            }
            return trim($dom->outerHtml);
        } catch (Exception $e) {
            Log::error('parse error', ['e' => $e->getMessage()]);
            return '';
        }
    }
}

if (!function_exists('get_age_by_idcard')) {
    function get_age_by_idcard($card)
    {
        if (!$card) {
            return '';
        }
        $id = $card;
        $birth_Date = strtotime(substr($id, 6, 8));
        $Year = date('Y', $birth_Date);
        $Month = date('m', $birth_Date);
        $Day = date('d', $birth_Date);

        $current_Y = date('Y');
        $current_M = date('m');
        $current_D = date('d');

        $age = $current_Y - $Year;
        if ($Month > $current_M || $Month == $current_M && $Day > $current_D) {
            $age--;
        }
        return $age;
    }
}

if (!function_exists('get_sex_by_idcard')) {
    function get_sex_by_idcard($card)
    {
        if (!$card || !is_numeric($card)) {
            return '';
        }
        if (strlen($card) === 15 || strlen($card) === 18) {
            $position = (strlen($card) === 15 ? -1 : -2);
            if (substr($card, $position, 1) == 'x' || substr($card, $position, 1) == 'X') {
                return '';
            }
            if (substr($card, $position, 1) % 2 == 0) {
                return '女';
            }
            return '男';
        } else {
            return '';
        }
    }
}

if (!function_exists('get_date_range')) {
    function get_date_range(Carbon $from, Carbon $to, $inclusive = true)
    {
        if ($from->gt($to)) {
            return null;
        }
        $from = $from->copy()->startOfDay();
        $to = $to->copy()->startOfDay();
        if ($inclusive) {
            $to->addDay();
        }
        $step = CarbonInterval::day();
        $period = new DatePeriod($from, $step, $to);
        $range = [];
        foreach ($period as $day) {
            $day = new Carbon($day);
            $range[] = $day;
        }
        return !empty($range) ? array_reverse($range) : null;
    }
}

if (!function_exists('check_date')) {
    function check_date($date)
    {
        if (empty($date)) {
            return false;
        }
        $r = false;
        if (date('Y-m-d', strtotime($date)) == $date) {
            $r = true;
        }
        if (date('Y/m/d', strtotime($date)) == $date) {
            $r = true;
        }
        return $r;
    }
}

if (!function_exists('change_star')) {
    function change_star($str, $start, $end, $sign = '*')
    {
        $start_str = mb_substr($str, 0, $start - 1);
        $end_str = mb_substr($str, $end - 1);
        $sign_str = '';
        for ($i = $start; $i < $end; $i++) {
            $sign_str .= $sign;
        }
        return $start_str . $sign_str . $end_str;
    }
}

function getBirthdayArr()
{
    return [
        '十一月三十' => [
            'name' => '英英酱',
        ],
        '十一月廿九' => [
            'name' => '英英酱',
        ],
        '一月十三'   => [
            'name' => '张龙',
        ],
        '一月十四'   => [
            'name' => '测试一下',
        ],
        '八月廿四'   => [
            'name' => '二蛋',
        ],
        '十月初五'   => [
            'name' => '刘丹丹',
        ],
        '一月初四'   => [
            'name' => '李国平',
        ],
        '一月十七'   => [
            'name' => '候文渊',
        ],
        '八月廿三'   => [
            'name' => '姜艳华',
        ],
        '九月十三'   => [
            'name' => '宋子东',
        ],
        '十月廿八'   => [
            'name' => '张欣欣',
        ],
        '八月初三'   => [
            'name' => '岳父大人',
        ],
        '十一月初三' => [
            'name' => '岳母大人',
        ],
        '一月廿九'   => [
            'name' => '大舅哥',
        ],
        '一月二十'   => [
            'name' => '测试一下',
        ],
    ];
}
